package com.example.myapplication.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.databinding.ActivityMain2Binding
import com.example.myapplication.kotlinroom.UserRoomK
import com.example.myapplication.kotlinroom.UserRoomKDao
import com.example.myapplication.kotlinroom.UserRoomKDb
import com.example.myapplication.kotlinroom.ioThread

class MainActivity2 : AppCompatActivity() {

    private var num: Int = 0

    private lateinit var binding: ActivityMain2Binding
        private set
    private  val userRoomKDao : UserRoomKDao by lazy { UserRoomKDb.get(application).userRoomKDao() }


    @SuppressLint("LongLogTag")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMain2Binding.inflate(layoutInflater)
        setContentView(binding.root)
//        userRoomKDao = UserRoomKDb.get(this).userRoomKDao()

        binding.btns.insert.setOnClickListener {
            ioThread {
                val listOf = mutableListOf<UserRoomK>()
                for (index in 1..100000) {
                    listOf.add(UserRoomK(0, "name+$index"))
                }
                val startTime = System.currentTimeMillis()
                userRoomKDao.insertAll(listOf)
                val endTime = System.currentTimeMillis() - startTime
                Log.e("Kotlin插入1w数据耗时:", (endTime.toString() + "ms"))
                addView("Kotlin插入10w数据耗时:" + (endTime.toString() + "ms"))
            }
        }

        binding.btns.delete.setOnClickListener {
            ioThread {
                val userRoomK = UserRoomK(num, "name")
                userRoomK.id = num;
                userRoomK.name = "name"
                val startTime = System.currentTimeMillis()
                userRoomKDao.delete(userRoomK)
                val endTime = System.currentTimeMillis() - startTime
                Log.e("Kotlin删除数据耗时:", endTime.toString() + "ms")
                addView("Kotlin删除数据耗时:" + endTime.toString() + "ms")
            }
        }

        binding.btns.update.setOnClickListener {
            ioThread {
                val userRoomK = UserRoomK(num, "name")
                userRoomK.id = num;
                userRoomK.name = "name"
                val startTime = System.currentTimeMillis()
                userRoomKDao.upDateOne(userRoomK)
                val endTime = System.currentTimeMillis() - startTime
                Log.e("Kotlin更新数据耗时:", endTime.toString() + "ms")
                addView("Kotlin更新数据耗时:" + endTime.toString() + "ms")
            }
        }

        binding.btns.query.setOnClickListener {
            ioThread {
                val startTime = System.currentTimeMillis()
                userRoomKDao.queryByAll()
                val endTime = System.currentTimeMillis() - startTime
                Log.e("Kotlin查询所有数据耗时:", endTime.toString() + "ms")
                addView("Kotlin查询所有数据耗时:" + endTime.toString() + "ms")
            }
        }

        binding.edtInput.input.addTextChangedListener(object : TextWatcher {
            var isEdt = false
            override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
            override fun afterTextChanged(editable: Editable) {
                if (isEdt) return
                isEdt = true
                val str = editable.toString().replace("/^0*/g", "")
                if (str != "") {
                    val i = str.toInt()
                    if (i != 0) num = i
                }
                isEdt = false
            }
        })
    }

    private fun addView(string: String) {
        runOnUiThread(Runnable {
            val textView = TextView(this)
            textView.text = string
            textView.textSize = 20f
            binding.texts.textss.addView(
                textView,
                ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
            )
        })
    }


}